package vip.xiaonuo.biz.modular.config.util;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import lombok.extern.slf4j.Slf4j;
import vip.xiaonuo.biz.modular.algorithmConfig.entity.AlgorithmConfig;

import java.util.List;

@Slf4j
public class HttpDreamLinkUtil {
    /**
     * 硬件服务调用
     *
     * @param algorithmConfigList
     * @param deviceSn
     */

    public static void httpDreamLink(List<AlgorithmConfig> algorithmConfigList, String deviceSn) {

        for (int i = 0; i < algorithmConfigList.size(); i++) {
            String service = "";
            JSONObject jsonObject = new JSONObject();
            if (algorithmConfigList.get(i).getName().equals("神经调控")) {
                JSONObject process = new JSONObject();
                process.putByPath("duration", algorithmConfigList.get(i).getDuration());
                process.putByPath("mode", algorithmConfigList.get(i).getMode());
                process.putByPath("amp", algorithmConfigList.get(i).getAmp());
                process.putByPath("frq", algorithmConfigList.get(i).getFrq());
                jsonObject.putByPath("process", process);
                jsonObject.putByPath("server", "tES");
                service = "tES";

            }


            if (algorithmConfigList.get(i).getName().equals("节拍振子助眠")) {
                JSONObject process = new JSONObject();
                process.putByPath("duration", algorithmConfigList.get(i).getDuration());
                process.putByPath("source", algorithmConfigList.get(i).getSource());
                process.putByPath("density", algorithmConfigList.get(i).getDensity());
                jsonObject.putByPath("process", process);
                jsonObject.putByPath("server", "vibrator");
                service = "vibrator";
            }


            if (algorithmConfigList.get(i).getName().equals("音频助眠")) {
                JSONObject process = new JSONObject();
                process.putByPath("time", algorithmConfigList.get(i).getDuration());
                process.putByPath("source", algorithmConfigList.get(i).getSource());
                process.putByPath("volume", algorithmConfigList.get(i).getVolume());

                jsonObject.putByPath("process", process);
                jsonObject.putByPath("server", "audio");
                service = "audio";
            }
            if (algorithmConfigList.get(i).getName().equals("双声拍助眠")) {
                JSONObject process = new JSONObject();
                process.putByPath("duration", algorithmConfigList.get(i).getDuration());
                process.putByPath("source", algorithmConfigList.get(i).getSource());
                process.putByPath("volume", algorithmConfigList.get(i).getVolume());
                jsonObject.putByPath("process", process);
                jsonObject.putByPath("server", "2beat");
                service = "2beat";
            }
            if (algorithmConfigList.get(i).getName().equals("LED灯带控制")) {
                JSONObject process = new JSONObject();
                process.putByPath("duration", algorithmConfigList.get(i).getDuration());
                process.putByPath("term", algorithmConfigList.get(i).getTerm());
                process.putByPath("mode", algorithmConfigList.get(i).getMode());
                process.putByPath("scolor", algorithmConfigList.get(i).getScolor());
                process.putByPath("ecolor", algorithmConfigList.get(i).getEcolor());
                jsonObject.putByPath("process", process);
                jsonObject.putByPath("server", "ledstrip");
                service = "ledstrip";
            }

            String productKey = MD5Encryption.encryptTo32BitMD5(deviceSn);
            String topic = "/thing/" + productKey + "/" + deviceSn + "/service/" + service;
            log.info("调用服务------"+jsonObject.toString());
            log.info("调用服务------主题"+topic);
            MqttUtil.publish(topic,jsonObject.toString());

           /* HttpRequest post = HttpUtil.createPost("https://dlink.suiren.com/api/biz/application/device/invoke/" + service + "/" + deviceSn).header("Content-Type", "application/json");
            post.body(jsonObject.toString());
            HttpResponse response = post.execute();
            log.info("调用服务------"+jsonObject.toString());
            if (!response.isOk()) {
                throw new RuntimeException("硬件服务调用:" + response.body());
            }*/


        }
    }
}
